package gogogo.liwushuo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.squareup.picasso.Picasso;

import java.util.List;

import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.HotFragmentBean;
import gogogo.liwushuo.listener.OnItemNameClickListener;

/**
 * author: John
 * time: 2016/7/21
 * overview:这只是演戏
 * usage:
 */
public class HotAdapter extends RecyclerView.Adapter<HotAdapter.HotHolder> {
    private List<HotFragmentBean.DataBean.ItemsBean> mDatas;
    private Context mContext;

    public HotAdapter(Context context, List<HotFragmentBean.DataBean.ItemsBean> datas) {
        this.mDatas = datas;
        this.mContext = context;
    }

    @Override
    public HotHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.hot_fragment_item, parent, false);

        return new HotHolder(view);
    }

    @Override
    public void onBindViewHolder(final HotHolder holder, final int position) {
        HotFragmentBean.DataBean.ItemsBean bean = mDatas.get(position);
        holder.tv_title.setText(bean.getData().getName());
        int favorites_count = bean.getData().getFavorites_count();
        String count = String.valueOf(favorites_count);
        holder.tv_count.setText(count);
        holder.tv_price.setText(bean.getData().getPrice());
        Picasso.with(mContext).load(bean.getData().getCover_image_url()).into(holder.iv_image);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(position,holder.itemView);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    class HotHolder extends RecyclerView.ViewHolder {
        private ImageView iv_image;
        private TextView tv_title;
        private TextView tv_price;
        private TextView tv_count;
        private CardView cardView;

        public HotHolder(View itemView) {
            super(itemView);
            cardView= (CardView) itemView.findViewById(R.id.hot_cv);
            iv_image = (ImageView) itemView.findViewById(R.id.hot_image);
            tv_count = (TextView) itemView.findViewById(R.id.hot_tv_count);
            tv_title = (TextView) itemView.findViewById(R.id.hot_tv_title);
            tv_price = (TextView) itemView.findViewById(R.id.hot_tv_price);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int pos, View itemView);
    }
    private OnItemClickListener mListener;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

}
